﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.contracts {
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class CreateContractCommand extends Command {

        public var event:InfoEvent;
        public var world:WorldModel;
        public var contractsModel:ContractsModel;
        private var productData:Object;

        override public function execute():void{
            this.productData = this.event.info;
            var _local1:Object = {taskType:GameConstants.ORDER_GOODS, itemType:GameConstants.ANY_TYPE, onResult:this.onResult, numActions:this.productData.amount};
            var _local2:RequestVO = new RequestVO("create_contract", {id:this.event.id, code:this.productData.code});
            var _local3:Array = [_local2];
            var _local4:TaskRequestEvent = new TaskRequestEvent(EventConstants.SEND_TASK_REQUEST, _local3, _local1);
            dispatch(_local4);
            this.contractsModel.state = ContractsModel.CREATING;
        }
        private function onResult(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:ContractVO;
            var _local4:String;
            var _local5:IArea;
            if (_arg1.success){
                _local2 = _arg1.data.create_contract;
                if (_local2.contract != "null"){
                    _local3 = new ContractVO();
                    _local3.id = _local2.contract.id;
                    _local3.goods = _local2.contract.goods;
                    _local3.readyAt = _local2.contract.ready_at;
                    _local4 = this.world.getRoomIdsOfType(GameConstants.TYPE_DOCK)[0];
                    _local5 = (this.world.getRoomById(_local4) as IArea);
                    _local5.contract = _local3;
                };
                dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, {changes:_local2.changes}));
            };
            dispatch(new InfoEvent(EventConstants.CREATE_CONTRACT_COMPLETE));
            this.track();
        }
        private function track():void{
            var _local1:ContractOfferVO = this.contractsModel.getContractOfferById(String(this.productData.code));
            var _local2:Object = {type:"order", st1:"coins", st2:_local1.goods, st3:(_local1.readyIn / 60)};
            dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.GOODS, _local2));
        }

    }
}//package net.wooga.pets.controllers.contracts 
